package com.authine.cloudpivot.web.api.secondopen.controller;

import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.modules.*;
import com.authine.cloudpivot.web.api.secondopen.service.QadInterfaceService;
import com.authine.cloudpivot.web.api.secondopen.utils.PageUtil;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;


/**
 * QAD接口对接控制器
 *
 */
@Api(value = "二开接口", tags = "QAD接口对接控制器")
@RestController
@RequestMapping("/api/qadInterface")
public class QadInterfaceController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(QadInterfaceController.class);

    @Autowired
    private QadInterfaceService qadInterfaceService;

    /**
     * 同步科目数据
     *
     */
//    @Scheduled(cron = "0 17 11 * * ?")
    @GetMapping("/syncSubjectData")
    public void syncSubjectData(){
        log.info("-----------同步科目数据开始------------");
        qadInterfaceService.syncSubjectDataInService();
        log.info("-----------同步科目数据结束------------");
    }


    /**
     * 同步供应商数据
     *
     */
//    @Scheduled(cron = "0 17 11 * * ?")
    @GetMapping("/syncSupplierData")
    public void syncSupplierData(){
        log.info("-----------同步供应商数据开始------------");
        qadInterfaceService.syncSupplierDataInService();
        log.info("-----------同步供应商数据结束------------");
    }

    /**
     * 获取供应商信息
     * @param qadVdAddr
     * @param qadVdBuyer
     * @param qadVdSort
     * @param qadVdDomain
     * @return
     */
    @RequestMapping(value = "/getProviderInfo", method = {RequestMethod.POST ,RequestMethod.GET })
    @ApiOperation(value = "获取供应商信息", notes = "获取供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadVdAddr", value = "供应商代码", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadVdBuyer", value = "采购员", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadVdSort", value = "Sort Name", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadVdDomain", value = "域", dataType = "String", required = false)
    })
    public ResponseResult<List<ProviderVO>> getProviderInfo(@RequestParam(value = "qadVdAddr", required = false) String qadVdAddr,
                                                            @RequestParam(value = "qadVdBuyer", required = false) String qadVdBuyer,
                                                            @RequestParam(value = "qadVdSort", required = false) String qadVdSort,
                                                            @RequestParam(value = "qadVdDomain", required = false) String qadVdDomain) {

        Map<String, String> params = new HashMap<>();
        if (!StringUtils.isBlank(qadVdAddr)) {
            params.put("qad_vd_addr", qadVdAddr);
        }
        if (!StringUtils.isBlank(qadVdBuyer)) {
            params.put("qad_vd_buyer", qadVdBuyer);
        }
        if (!StringUtils.isBlank(qadVdSort)) {
            params.put("qad_vd_sort", qadVdSort);
        }
        if (!StringUtils.isBlank(qadVdDomain)) {
            params.put("qad_vd_domain", qadVdDomain);
        }

        List<ProviderVO> providerVOs = qadInterfaceService.providerInfoGet(params);
        if (!org.apache.commons.collections.CollectionUtils.isEmpty(providerVOs)) {
            return getOkResponseResult(providerVOs, "获取供应商信息成功");
        }
        return getErrResponseResult(null, 999999L, "获取供应商信息失败");
    }

    /**
     * 获取客户信息
     * @param qadCmAddr
     * @param qadCmShip
     * @param qadCmBill
     * @param qadCmDomain
     * @return
     */
    @RequestMapping(value = "/getCustomerInfo", method = {RequestMethod.POST ,RequestMethod.GET })
    @ApiOperation(value = "获取客户信息", notes = "获取客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadCmAddr", value = "客户代码", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadCmShip", value = "发货至代码", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadCmBill", value = "开票至", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadCmDomain", value = "域", dataType = "String", required = false)
    })
    public ResponseResult<List<CustomerVO>> getCustomerInfo(@RequestParam(value = "qadCmAddr", required = false) String qadCmAddr,
                                                            @RequestParam(value = "qadCmShip", required = false) String qadCmShip,
                                                            @RequestParam(value = "qadCmBill", required = false) String qadCmBill,
                                                            @RequestParam(value = "qadCmDomain", required = false) String qadCmDomain) {

        Map<String, String> params = new HashMap<>();
        if (!StringUtils.isBlank(qadCmAddr)) {
            params.put("qad_cm_addr", qadCmAddr);
        }
        if (!StringUtils.isBlank(qadCmShip)) {
            params.put("qad_cm_ship", qadCmShip);
        }
        if (!StringUtils.isBlank(qadCmBill)) {
            params.put("qad_cm_bill", qadCmBill);
        }
        if (!StringUtils.isBlank(qadCmDomain)) {
            params.put("qad_cm_domain", qadCmDomain);
        }


        List<CustomerVO> customerVOs = qadInterfaceService.customerInfoGet(params);
        if (!org.apache.commons.collections.CollectionUtils.isEmpty(customerVOs)) {
            return getOkResponseResult(customerVOs, "获取供应商信息成功");
        }
        return getErrResponseResult(null, 999999L, "获取供应商信息失败");
    }


    /**
     * 根据主账户-分账户-成本中心，查询项目信息
     *
     * @param oneSubjectCode
     * @param twoSubjectCode
     * @param cbzx
     * @return
     */
    @RequestMapping(value = "/getProductNoByParam", method = {RequestMethod.POST ,RequestMethod.GET })
    @ApiOperation(value = "根据主账户-分账户-成本中心，查询项目信息", notes = "根据主账户-分账户-成本中心，查询项目号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oneSubjectCode", value = "主账户编码", dataType = "String", required = false),
            @ApiImplicitParam(name = "twoSubjectCode", value = "分帐户编码", dataType = "String", required = false),
            @ApiImplicitParam(name = "cbzx", value = "成本中心", dataType = "String", required = false)
    })
    public ResponseResult<ProductDeatailVO> getProductNoByParam(@RequestParam(value = "oneSubjectCode", required = false) String oneSubjectCode,
                                                                @RequestParam(value = "twoSubjectCode", required = false) String twoSubjectCode,
                                                                @RequestParam(value = "cbzx", required = false) String cbzx,
                                                                @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                                @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        ProductDeatailVO productDeatailVO=new ProductDeatailVO();
        if(StringUtils.isBlank(oneSubjectCode) || StringUtils.isBlank(twoSubjectCode) || StringUtils.isBlank(cbzx)){
            log.info("------------参数不全------------参数信息为：oneSubjectCode：{},twoSubjectCode：{},cbzx：{}",oneSubjectCode,twoSubjectCode,cbzx);
            productDeatailVO.setTotal(0);
            productDeatailVO.setProductVOs(new ArrayList<>());
            return getOkResponseResult(new ProductDeatailVO(), "参数不全");

        }
        List<ProductVO> productNos = qadInterfaceService.getProductNoByParamInterface(oneSubjectCode,twoSubjectCode,cbzx);
        productDeatailVO.setTotal(productNos.size());
        if(CollectionUtils.isNotEmpty(productNos)){
            productDeatailVO.setProductVOs(PageUtil.startPage(productNos,pageNum+1,pageSize));
        }else{
            productDeatailVO.setProductVOs(new ArrayList<>());
        }
        return getOkResponseResult(productDeatailVO, "获取项目号信息成功");
    }


    /**
     * 查询当天汇率信息
     *
     * @param qadExrCurrZf
     * @param qadExrCurrBzf
     * @return
     */
    @RequestMapping(value = "/getZshlByParam", method = {RequestMethod.POST ,RequestMethod.GET })
    @ApiOperation(value = "查询折算汇率信息", notes = "查询折算汇率信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadExrCurrZf", value = "转方", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadExrCurrBzf", value = "被转方", dataType = "String", required = false)
    })
    public AjaxResult getZshlByParam(@RequestParam(value = "qadExrCurrZf", required = false) String qadExrCurrZf,
                                     @RequestParam(value = "qadExrCurrBzf", required = false) String qadExrCurrBzf) {

        if(StringUtils.isBlank(qadExrCurrZf) || StringUtils.isBlank(qadExrCurrBzf)){
            log.info("------------参数不全------------参数信息为：qadExrCurrZf：{},qadExrCurrBzf：{}",qadExrCurrZf,qadExrCurrBzf);
            return AjaxResult.error(300,"请求参数不全");
        }
        BigDecimal rate = qadInterfaceService.getQadZshlByParam(qadExrCurrZf,qadExrCurrBzf);
        return AjaxResult.success(rate);
    }

    @RequestMapping(value = "/getAdAddrAndNames", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取供应商编码、名称", notes = "获取供应商编码、名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadAdName", value = "名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAdType", value = "类型", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAdAddr", value = "地址代码", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageSize", value = "条数", dataType = "String", required = false)
    })
    public ResponseResult<QadGysOrKhVO> getAdAddrAndNames(@RequestParam(value = "qadAdName", required = false) String qadAdName,
                                                          @RequestParam(value = "qadAdType", required = false) String qadAdType,
                                                          @RequestParam(value = "qadAdAddr", required = false) String qadAdAddr,
                                                          @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Map<String, String> params = new HashMap<>();
        if (!StringUtils.isBlank(qadAdName)) {
            params.put("qad_ad_name", qadAdName);
        }
        if (!StringUtils.isBlank(qadAdType)) {
            params.put("qad_ad_type", qadAdType);
        }
        if (!StringUtils.isBlank(qadAdAddr)) {
            params.put("qad_ad_addr", qadAdAddr);
        }
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 1;
        }
        List<AdAddrAndNameVO> adAddrAndNameVOs = qadInterfaceService.adAddrAndNamesGet(params);
        QadGysOrKhVO qadGysOrKhVO = new QadGysOrKhVO();
        qadGysOrKhVO.setTotal(adAddrAndNameVOs.size());
        if (CollectionUtils.isNotEmpty(adAddrAndNameVOs)) {
            qadGysOrKhVO.setAdAddrAndNameVOs(PageUtil.startPage(adAddrAndNameVOs, pageNum + 1, pageSize));
        }
        return getOkResponseResult(qadGysOrKhVO, "获取供应商编码、姓名成功");
    }


    @RequestMapping(value = "/getAdAddrAndNamesInLoad", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取供应商编码、名称，用于load方法", notes = "获取供应商编码、名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadAdName", value = "名称", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAdType", value = "类型", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadAdAddr", value = "地址代码", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageSize", value = "条数", dataType = "String", required = false)
    })
    public ResponseResult<AdAddrAndNameVO> getAdAddrAndNamesInLoad(@RequestParam(value = "qadAdType", required = false) String qadAdType,
                                                          @RequestParam(value = "qadAdAddr", required = false) String qadAdAddr) {

        AdAddrAndNameVO adAddrAndNameVO = new AdAddrAndNameVO();
        if(StringUtils.isBlank(qadAdType) || StringUtils.isBlank(qadAdAddr)){
            log.info("参数不全，qadAdType:{},qadAdAddr:{}",qadAdType,qadAdAddr);
            return getOkResponseResult(adAddrAndNameVO, "获取供应商编码、姓名成功");
        }
        Map<String, String> params = new HashMap<>();
        params.put("qad_ad_type", qadAdType);
        params.put("qad_ad_addr", qadAdAddr);

        List<AdAddrAndNameVO> adAddrAndNameVOs = qadInterfaceService.adAddrAndNamesGet(params);
        if (CollectionUtils.isNotEmpty(adAddrAndNameVOs)) {
            adAddrAndNameVO=adAddrAndNameVOs.get(0);
        }
        return getOkResponseResult(adAddrAndNameVO, "获取供应商编码、姓名成功");
    }

    @RequestMapping(value = "/getCreditorType", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "获取供应商类型", notes = "获取供应商类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "供应商类型编码", dataType = "String", required = false),
            @ApiImplicitParam(name = "description", value = "类型描述", dataType = "String", required = false)
    })
    public ResponseResult<List<CreditorTypeVO>> getCreditorType(@RequestParam(value = "code", required = false) String code,
                                                                @RequestParam(value = "description", required = false) String description) {
        List<CreditorTypeVO> creditorTypeVOs = new ArrayList<>();
        Map<String, String> params = new HashMap<>();
        if (!StringUtils.isBlank(code)) {
            params.put("qad_creditortype_creditortypecode", code);
        }
        if (!StringUtils.isBlank(description)) {
            params.put("qad_creditortype_creditortypedescription", description);
        }
        creditorTypeVOs = qadInterfaceService.getCreditorType(params);
        if (CollectionUtils.isNotEmpty(creditorTypeVOs)) {
            return getOkResponseResult(creditorTypeVOs, "获取供应商类型成功");
        }
        return getErrResponseResult(null, 999999L, "获取供应商类型失败");
    }

    @RequestMapping(value = "/getQadCInvoice", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "根据起始时间，供应商编码类型查询客户发票信息", notes = "根据起始时间，供应商编码类型查询客户发票信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", required = false),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", required = false),
            @ApiImplicitParam(name = "gysCodeType", value = "供应商编码类型", dataType = "String", required = false)
    })
    public AjaxResult getQadCInvoice(@RequestParam(value = "startDate", required = false) String startDate,
                                     @RequestParam(value = "endDate", required = false) String endDate,
                                     @RequestParam(value = "gysCodeType", required = false) String gysCodeType,
                                     @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if(startDate==null || endDate==null || StringUtils.isBlank(gysCodeType)){
            log.info("---------------参数不全,参数为：startDaocrInvoiceDatate:{},endDate:{},gysCodeType{}------------------",startDate,endDate,gysCodeType,pageNum,pageSize);
            return AjaxResult.error(200, "参数不全");
        }
        List<QadCInvoiceDeatail> qadCInvoiceDeatails = qadInterfaceService.getQadCInvoiceByParam(startDate,endDate,gysCodeType);
        QadCInvoice qadCInvoice =new QadCInvoice();
        qadCInvoice.setTotal(qadCInvoiceDeatails.size());

        if(CollectionUtils.isNotEmpty(qadCInvoiceDeatails) && pageSize!=null && pageSize!=0) {
            qadCInvoice.setQadCInvoices(PageUtil.startPage(qadCInvoiceDeatails, pageNum, pageSize));
        }else{
            qadCInvoice.setQadCInvoices(qadCInvoiceDeatails);
        }

        List<QadCInvoiceDeatail> removalQadCInvoices=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(qadCInvoiceDeatails)){
            removalQadCInvoices=qadInterfaceService.removalQadCInvoiceDeatails(qadCInvoiceDeatails);
            qadCInvoice.setRemovalQadCInvoices(removalQadCInvoices);
        }
        return AjaxResult.success(qadCInvoice);
    }

    /**
     * 根据客户代码查询销售订单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getSalesOrderByAdAddr", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "根据客户代码获取商品编码、名称、单位、价格", notes = "根据客户代码获取商品编码、名称、单位、价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadPiCsCode", value = "客户代码", dataType = "String", required = false),
            @ApiImplicitParam(name = "qadPiPartCode",value = "商品编码" ,dataType = "String",required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "pageSize", value = "条数", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "time", value = "当前时间", dataType = "Date", required = false)
    })
    public ResponseResult<QadSalesOrderVO> getSalesOrderByAdAddr(@RequestParam(value = "qadPiCsCode", required = false) String qadPiCsCode,
                                                          @RequestParam(value = "qadPiPartCode",required = false) String qadPiPartCode,
                                                          @RequestParam(value = "time",required = false) Date time,
                                                          @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                          @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Map<String, String> params = new HashMap<>();
        if (!StringUtils.isBlank(qadPiPartCode)) {
            params.put("qad_pi_part_code", qadPiPartCode);
        }
        if (!StringUtils.isBlank(qadPiCsCode)) {
            params.put("qad_pi_cs_code", qadPiCsCode);
        }
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 1;
        }
        List<SalesOrderVO> salesOrderVOS = qadInterfaceService.salesOrderGet(params,time);
        QadSalesOrderVO qadSalesOrderVO = new QadSalesOrderVO();
        qadSalesOrderVO.setTotal(salesOrderVOS.size());
        if (CollectionUtils.isNotEmpty(salesOrderVOS)) {
            qadSalesOrderVO.setSalesOrderVOS(PageUtil.startPage(salesOrderVOS, pageNum + 1, pageSize));
        }
        return getOkResponseResult(qadSalesOrderVO, "获取销售订单成功");

    }

    /**
     * 查找所有库位
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getAllLoc", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "查找所有库位", notes = "查找所有库位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qadLoc", value = "库位", dataType = "String", required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "pageSize", value = "条数", dataType = "Integer", required = false)
    })
    public ResponseResult<QadLocVO> getLoc(@RequestParam(value = "qadLoc", required = false) String qadLoc,
                                                                 @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                                 @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 1;
        }
        List<LocVO> locVOS = qadInterfaceService.getAllLoc(qadLoc);
        QadLocVO qadLocVO = new QadLocVO();
        qadLocVO.setTotal(locVOS.size());
        if (CollectionUtils.isNotEmpty(locVOS)) {
            qadLocVO.setLocVOS(PageUtil.startPage(locVOS, pageNum + 1, pageSize));
        }
        return getOkResponseResult(qadLocVO, "获取所有库位成功");

    }




}
